import store from "@/store/index.js"
import {CURRENT_TRAVEL} from "@/store/mutations_type.js"
import * as util from '@/utils/util'
import * as Travel from "@/core/constant/travel.js"
import toast from "../uni_modules/uview-ui/libs/config/props/toast"
import storage from "@/utils/storage.js"
/**
 * 显示成功提示框
 */
export const showSuccess = (config, callback) => {
  uni.showToast({
    title: config.msg ?? "",
    icon: 'success',
    duration: 1500,
    success() {
      callback && callback()
	  
	  if(config.url !== ""){
	  	navTo(config.url)
	  }
    }
  })
}

export const showConfirm = (config,resolve,reject) => {
  uni.showModal({
  	title: '提示',
  	content: config.content,
	cancelText:config.cancel ?? "取消",
	confirmText:config.confirm ?? "确定",
	confirmColor:"#d9480f",
  	success: function (res) {
  		if (res.confirm) {
  			resolve && resolve()
  		} else if (res.cancel) {
			reject && reject()
  		}
		if(config.url !== ""){
			navTo(config.url)
		}
  	}
  });
}

/**
 * 显示失败提示框
 * 失败情况：程序异常
 */
export const showError = (msg, callback) => {
  uni.showModal({
    title: '来自提醒',
    content: msg,
    showCancel: false,
    success(res) {
      callback && callback()
    }
  })
}

/**
 * 显示纯文字提示框
 */
export const showToast = (msg, duration = 1500, mask = true) => {
  uni.showToast({
    title: msg, // 提示的内容
    icon: 'none',
    mask, // 是否显示透明蒙层，防止触摸穿透
    duration // 提示的延迟时间，单位毫秒，默认：1500  
  })
}


/**
 * tabBar页面路径列表 (用于链接跳转时判断)
 * tabBarLinks为常量, 无需修改
 */
export const getTabBarLinks = () => {
  const tabBarLinks = [
    'pages/index/index',
    'pages/xzhu/xzhu',
    'pages/chux/chux',
    'pages/my/my',
  ]
  return tabBarLinks
}

export const speak = (src) =>{
	store.dispatch("speaker/speak",src)
}


/**
 * 跳转到指定页面url
 * 支持tabBar页面
 * @param {string}  url   页面路径
 * @param {object}  query 页面参数
 * @param {string}  modo  跳转类型(默认navigateTo)
 */
export const navTo = (url, query = {}, modo = 'navigateTo') => {
  if (!url || url.length == 0) {
    return false
  }
  // tabBar页面, 使用switchTab
  const tabBarLinks = getTabBarLinks()
  if (tabBarLinks.includes(url)) {
    uni.switchTab({
      url: `/${url}`
    })
    return true
  }
  // 生成query参数
  const queryStr = !util.isEmpty(query) ? '?' + util.urlEncode(query) : ''
  // 普通页面, 使用navigateTo
  modo === 'navigateTo' && uni.navigateTo({
    url: `/${url}${queryStr}`
  })
  // 特殊指定, 使用redirectTo
  modo === 'redirectTo' && uni.redirectTo({
    url: `/${url}${queryStr}`
  })
  return true
}


export const navToTravel = () => {
	const travel = store.getters.currentTravel
	console.log(travel)
	if(!travel){
		showError("当前没有正在进行的出行")
	}
	const options = {
		travelId:travel.id ?? travel.travelId
	}
	switch(travel.status){
		case Travel.TRAVEL_WAITING:
			navTo("pages/ongoing/ongoing",options)
			break
		case Travel.TRAVEL_GOING:
			navTo("pages/track/track",options)
			break
		case Travel.TRAVEL_DONE:
			navTo("pages/comment/comment",options)
			break
		default:
			storage.remove(CURRENT_TRAVEL)
	}
}


export const checkLogin = () => {
	return store.getters.token !== ""
}


export const checkTaskCompleted = () => {
	const role = checkRole()
	const complete = store.getters.complete
	return role && complete
	
}

export const checkRole = () => {
	// 0: 没登录或没认证
	if(store.getters.role === 0 || !checkLogin()){
		return 0
	}
	
	// 1: 登录且身份是志愿者	
	if(store.getters.role === 1 && checkLogin()){
		return 1
	}
	
	// 2: 登录且身份是盲人
	if(store.getters.role === 2 && checkLogin()){
		return 2
	}
}




